﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class GrassSlot : EventTrigger
{
    private GrassGrid mGrassGrid { get; set; }

    private void Start()
    {
        mGrassGrid = this.GetComponentInParent<GrassGrid>();
    }    

    public override void OnPointerEnter(PointerEventData eventData)
    {
        CardMgr mCardMgr = CardMgr.GetInstance;
        if (mCardMgr.mCardGameobject)
        {
            //已经选择了植物
            mGrassGrid.SelectSlot(GetAnchorPosition());
        }
    }

    public override void OnPointerUp(PointerEventData eventData)
    {

        GameObject triggerObj = CardMgr.GetInstance.mCardGameobject;
        if (this.transform.childCount > 0 && !triggerObj)
        {
            return;
        }

        //冷却        
        if (triggerObj)
        {
            CardTriggerListener trigger = triggerObj.GetComponent<CardTriggerListener>(); 
            Action coldAction = trigger.mAction;
            if (coldAction != null)
            {
                //冷却
                coldAction();
            }

            //确定在该草地槽里添加植物
            LoadPlant(trigger.mName);
        }
        CardMgr.GetInstance.mCardGameobject = null;
        mGrassGrid.InitGrid();
    }

    public void LoadPlant(string plantPath)
    {
        GameObject shooterPlant = Resources.Load<GameObject>(plantPath);
        shooterPlant = Instantiate(shooterPlant);
        shooterPlant.transform.SetParent(this.transform, false);
    }

    public Vector2 GetAnchorPosition()
    {
        return GetComponent<RectTransform>().anchoredPosition;
    }

    /// <summary>
    /// 未被选中状态
    /// </summary>
    public void SetCancelState()
    {
        Color color = this.GetComponent<Image>().color;
        color.a = 0f;
        this.GetComponent<Image>().color = color;
    }

    /// <summary>
    /// 选中状态
    /// </summary>
    public void SetActState()
    {
        Color color = this.GetComponent<Image>().color;
        color.a = 0.5f;
        this.GetComponent<Image>().color = color;
    }
}
